#!/bin/sh -e
### BEGIN INIT INFO
# Provides:          magenta
# Required-Start:    ifupdown-clean
# Required-Stop:     $local_fs
# Default-Start:     S
# Default-Stop:      0 6
# Short-Description: Load the firewall rules.
### END INIT INFO

PROGRAM="/usr/sbin/magenta"
[ -x "$PROGRAM" ] || exit 0
RULES_OFF="/etc/network/off.magenta"
RULES_ON="/etc/network/on.magenta"

State () {
	case "$1" in
		'closed') RULES="$RULES_OFF" ;;
		'opened') RULES="$RULES_ON" ;;
		*) return 255;;
	esac
	echo -n "Putting firewall in $1 state... "
	if [ -r "$RULES" ]; then
		if "$PROGRAM" < "$RULES"; then
			echo "done."
		else
			echo "FAIL: [$PROGRAM] returned an error."
			exit 255
		fi
	else
		echo "FAIL: rules file [$RULES] can't be read."
		exit 255
	fi
}

case "$1" in
	'stop')                           State "closed";;
	'start'|'restart'|'force-reload') State "closed"; State "opened";;
	*)
		echo "Usage: $0 { start | stop | restart }"
		exit 255
	;;
esac

exit 0
